package com.shanghai.shanghaielectric.UI.chart;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.shanghai.shanghaielectric.Model.CapacityCuttingModel;
import com.shanghai.shanghaielectric.Model.EventBus.EventBusModel_Date;
import com.shanghai.shanghaielectric.R;
import com.shanghai.shanghaielectric.UI.DilaogView.Dialog_Alert;
import com.shanghai.shanghaielectric.UI.DilaogView.Dialog_datePickUtil;
import com.shanghai.shanghaielectric.UI.refreshView.MyAdapter_capacity_cutting;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.litepal.crud.DataSupport;
import org.litepal.crud.callback.FindMultiCallback;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import shanghaielectric_lib.Utils.DateUtil;

/**
 * 电容投切界面
 */
public class Chart_capacityCuttingFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener, OnClickListener {
    public View eventWarningView;
    @BindView(R.id.bt_startTime_cutting)
    TextView btStartTimeCutting;
    @BindView(R.id.bt_endTime_cutting)
    TextView btEndTimeCutting;
    @BindView(R.id.bt_query_cutting)
    Button btQueryCutting;
    @BindView(R.id.bt_output_cutting)
    Button btOutputCutting;
    @BindView(R.id.bt_clearData_cutting)
    Button btClearDataCutting;
    @BindView(R.id.item_id)
    TextView itemId;
    @BindView(R.id.item_Date)
    TextView itemDate;
    @BindView(R.id.item_GB_1)
    TextView itemGB1;
    @BindView(R.id.item_GB_2)
    TextView itemGB2;
    @BindView(R.id.item_GB_3)
    TextView itemGB3;
    @BindView(R.id.item_GB_4)
    TextView itemGB4;
    @BindView(R.id.item_FB1_A)
    TextView itemFB1A;
    @BindView(R.id.item_FB1_B)
    TextView itemFB1B;
    @BindView(R.id.item_FB1_C)
    TextView itemFB1C;
    @BindView(R.id.item_FB2_A)
    TextView itemFB2A;
    @BindView(R.id.item_FB2_B)
    TextView itemFB2B;
    @BindView(R.id.item_FB2_C)
    TextView itemFB2C;
    @BindView(R.id.item_FB3_A)
    TextView itemFB3A;
    @BindView(R.id.item_FB3_B)
    TextView itemFB3B;
    @BindView(R.id.item_FB3_C)
    TextView itemFB3C;
    @BindView(R.id.recyclerview_cutting)
    RecyclerView recyclerviewCutting;
    @BindView(R.id.layout_swipe_refresh_cutting)
    SwipeRefreshLayout layoutSwipeRefreshCutting;

    Unbinder unbinder;
    private RadioGroup tabs_warning_rg;
    private LayoutInflater myInflater;
    private Context mContext;

    private List<CapacityCuttingModel> capacityCuttingModels;
    private String startDate, endDate;
    private MyAdapter_capacity_cutting mAdapter;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        System.out.println("EventWarning____onAttach");
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.out.println("EventWarning____onCreate");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        System.out.println("EventWarning____onCreateView");
        eventWarningView = inflater.inflate(R.layout.chart_capacity_cutting_fragment, container, false);
        mContext = getActivity().getApplicationContext().getApplicationContext();
//		myInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        unbinder = ButterKnife.bind(this, eventWarningView);

        initData();
        initView();
        initListener();
        return eventWarningView;
    }

    private void initListener() {
        EventBus.getDefault().register(this);

    }

    //初始化一开始加载的数据
    private void initData() {
        startDate = DateUtil.getSystemTime_yyyy_MM_dd_yesterday();
        endDate = DateUtil.getSystemTime_yyyy_MM_dd();

        long startTime = DateUtil.transferStringDate_yyyy_MM_dd_long(startDate);
        long endTime = DateUtil.transferStringDate_yyyy_MM_dd_long(endDate);

        capacityCuttingModels = new ArrayList<>();
        DataSupport.where("date > ? and date < ?", startTime + "", endTime + "").limit(100).order("date desc").findAsync(CapacityCuttingModel.class).listen(new FindMultiCallback() {
            @Override
            public <T> void onFinish(List<T> t) {
//                (ErrorInfoModel)t;
                capacityCuttingModels.clear();
                capacityCuttingModels.addAll((List<CapacityCuttingModel>) t);
                Log.e("chartreportt", "查询到" + t.size() + "条数据");
                updateData();

//				Toast.makeText(MyApplication.getContext(),"saveAsync"+t.size(),Toast.LENGTH_LONG).show();
            }
        });//asc 正序
    }

    public void initView() {
        btStartTimeCutting.setText(startDate);
        btEndTimeCutting.setText(endDate);
        mAdapter = new MyAdapter_capacity_cutting(mContext, capacityCuttingModels);

        //这个是下拉刷新出现的那个圈圈要显示的颜色
        layoutSwipeRefreshCutting.setColorSchemeResources(
                R.color.red_color,
                R.color.yellow_color,
                R.color.green_color
        );
    }


    /**
     * 重写SwipeRefreshLayout.OnRefreshListener的OnRefresh方法
     * 在这里面去做下拉刷新的操作
     */
    @Override
    public void onRefresh() {


    }

    private void updateData() {

        mAdapter.notifyDataSetChanged();
        layoutSwipeRefreshCutting.setRefreshing(false);

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        System.out.println("EventWarning____onActivityCreated");
    }

    @Override
    public void onStart() {
        super.onStart();
        System.out.println("EventWarning____onStart");
    }

    @Override
    public void onResume() {
        super.onResume();
//        updateData();
//        DataSupport.limit(100).order("alertDate desc").findAsync(WarningInfoModel.class).listen(new FindMultiCallback() {
//            @Override
//            public <T> void onFinish(List<T> t) {
////                (ErrorInfoModel)t;
//                warningInfoModels.clear();
//                warningInfoModels.addAll(0, (List<WarningInfoModel>) t);
//                mAdapter.notifyDataSetChanged();
//
//
//            }
//        });//asc 正序
        System.out.println("EventWarning____onResume");
    }

    @Override
    public void onPause() {
        super.onPause();

        System.out.println("EventWarning____onPause");
    }

    @Override
    public void onStop() {
        super.onStop();
        System.out.println("EventWarning____onStop");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);

        System.out.println("EventWarning____onDestroyView");
        unbinder.unbind();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        System.out.println("EventWarning____onDestroy");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        System.out.println("EventWarning____onDetach");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {

        }
    }


    private void showDiolg(String commandName) {
        Dialog_Alert diolgAlert = new Dialog_Alert();
        diolgAlert.setCommandName(commandName);
        diolgAlert.show(this.getFragmentManager(), "");
    }

    @OnClick({R.id.bt_startTime_cutting, R.id.bt_endTime_cutting, R.id.bt_query_cutting, R.id.bt_output_cutting, R.id.bt_clearData_cutting, R.id.recyclerview_cutting, R.id.layout_swipe_refresh_cutting})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_startTime_cutting:
                showDiolg_datePick("请选择起始时间");
                Dialog_datePickUtil.setDiolgAlertListener(new Dialog_datePickUtil.Diolg_datePickListener() {
                    @Override
                    public void onClink(String date) {
                        postEventDate(date, EventBusModel_Date.START_DATE);
                        Toast.makeText(mContext, "" + date, Toast.LENGTH_SHORT).show();
                    }
                });
                break;
            case R.id.bt_endTime_cutting:

                showDiolg_datePick("请选择截止时间");
                Dialog_datePickUtil.setDiolgAlertListener(new Dialog_datePickUtil.Diolg_datePickListener() {
                    @Override
                    public void onClink(String date) {
                        postEventDate(date, EventBusModel_Date.END_DATE);
                        Toast.makeText(mContext, "" + date, Toast.LENGTH_SHORT).show();
                    }
                });
                break;
            case R.id.bt_query_cutting:


                long startTime = DateUtil.transferStringDate_yyyy_MM_dd_HH_00_long(startDate);
                long endTime = DateUtil.transferStringDate_yyyy_MM_dd_HH_00_long(endDate);
                Log.e("chartreportt6", "查询数据" + startTime + "----" + endTime);

                DataSupport.where("date > ? and date < ?", startTime + "", endTime + "").order("date desc").findAsync(CapacityCuttingModel.class).listen(new FindMultiCallback() {
                    @Override
                    public <T> void onFinish(List<T> t) {
                        capacityCuttingModels.clear();
                        capacityCuttingModels.addAll((List<CapacityCuttingModel>) t);
                        Log.e("chartreportt6", "查询到" + t.size() + "条数据");
                        updateData();

                    }
                });//asc 正序

                break;
            case R.id.bt_output_cutting:
                break;
            case R.id.bt_clearData_cutting:
                break;
            case R.id.recyclerview_cutting:
                break;
            case R.id.layout_swipe_refresh_cutting:
                break;
        }
    }

    private int year, month, day;
    private Calendar cal;

    //获取当前日期
    private void getDate() {
        cal = Calendar.getInstance();
        year = cal.get(Calendar.YEAR);       //获取年月日时分秒
        Log.i("wxy", "year" + year);
        month = cal.get(Calendar.MONTH);   //获取到的月份是从0开始计数
        day = cal.get(Calendar.DAY_OF_MONTH);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onDataUpdate(EventBusModel_Date eventBusModel_date) {
        if (eventBusModel_date.fragmentName.equals(Chart_capacityCuttingFragment.class.getName()))
            if (EventBusModel_Date.START_DATE.equals(eventBusModel_date.dateType)) {
                startDate = eventBusModel_date.date;
                btStartTimeCutting.setText(startDate);
            } else if (EventBusModel_Date.END_DATE.equals(eventBusModel_date.dateType)) {
                endDate = eventBusModel_date.date;
                btEndTimeCutting.setText(endDate);

            }
    }

    private void postEventDate(String date, String type) {
        EventBusModel_Date eventBusModel_date = new EventBusModel_Date();
        eventBusModel_date.fragmentName = Chart_capacityCuttingFragment.class.getName();
        eventBusModel_date.dateType = type;
        eventBusModel_date.date = date;
        EventBus.getDefault().post(eventBusModel_date);
    }

    private void showDiolg_datePick(String commandName) {
        Dialog_datePickUtil datePickUtil = new Dialog_datePickUtil();
        datePickUtil.setCommandName(commandName,Dialog_datePickUtil.yyyy_MM_dd);
        datePickUtil.show(this.getFragmentManager(), "");
    }

}
